package com.atguigu.yygh.order.controller;


import com.atguigu.common.result.Result;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.util.AuthContextHolder;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-06-17
 */
@RestController
@RequestMapping("/api/order/")
public class OrderInfoController {


    @Autowired
    OrderInfoService orderInfoService;

    @PostMapping("getCountMap")
    public Map<String,Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderInfoService.getCountMap(orderCountQueryVo);
    }

    @GetMapping("cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
        orderInfoService.cancelOrder(orderId);
        return  Result.ok();
    }



    @PostMapping("getPageList/{pageNum}/{pageSize}")
    public Result getPageList(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody OrderQueryVo orderQueryVo , HttpServletRequest request){

        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);

        Page<OrderInfo> page = orderInfoService.selectPage(pageNum,pageSize,orderQueryVo);

        return Result.ok().data("pageModel",page);
    }


    @GetMapping("/getStatusList")
    public Result getStatusList(){
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return Result.ok().data("statusList",statusList);
    }

    /**
     *
     * @param scheduleId 排班id（mongodb中存储的）
     * @param patientId 就诊人id
     * @return 返回平台端的订单id
     */
    @PostMapping("/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId){
        Long orderid = orderInfoService.submitOrder(scheduleId,patientId);
        return Result.ok().data("orderId",orderid);//平台端（尚医通）创建的订单的id -- `yygh_order`.`order_info`（下一个页面根据订单id查询订单详情）
    }



    @GetMapping("getOrderById/{orderId}")
    public Result getOrderById(@PathVariable Long orderId){
        OrderInfo orderInfo = orderInfoService.getOrderById(orderId);
        return Result.ok().data("orderInfo",orderInfo);
    }

}

